﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 暑假作业
{
    public partial class goodsInfo : Form
    {
        public goodsInfo()
        {
            InitializeComponent();
        }
        //展示数据
        private void goodsInfo_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            var sql = "select  goodsid,GName,UnitPrice,Manufactrue,category from goods";
            var ds = DBHelper.show(sql);
            dataGridView1.DataSource = ds.Tables[0];
        }
        //关闭返回主界面
        private void goodsInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }
        //添加窗体
        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            goodsInfo_add goodsInfo_Add = new goodsInfo_add();
            goodsInfo_Add.goodsInfo = this;
            goodsInfo_Add.Show();
        }
        //修改窗体
        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                goodsInfo_update goodsInfo_Update = new goodsInfo_update();
                goodsInfo_Update.goodsInfo = this;
                goodsInfo_Update.id = Convert.ToInt32(dataGridView1.CurrentRow.Index);
                goodsInfo_Update.Show();
            }
            else
            {
                MessageBox.Show("请选中");
            }
        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            var a = textBox1.Text;
            var b = textBox2.Text;
            if (textBox1.Text.Trim() != "" && comboBox1.SelectedIndex == 0)
            {
                var sql = $"select goodsid,GName,UnitPrice,Manufactrue,category from goods where goodsid like '%{a}%'";
                var ds = DBHelper.show(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0];
                }
                else
                {
                    MessageBox.Show("请输入正确信息后查询");
                    goodsInfo_Load(null, null);
                    textBox1.Text = "";
                }
            }
           if (textBox2.Text.Trim() != "" && comboBox1.SelectedIndex == 0)
            {
                var sql1 = $"select goodsid,GName,UnitPrice,Manufactrue,category from goods where gname like '%{b}%'";
                var ds1 = DBHelper.show(sql1);
                dataGridView1.DataSource = ds1.Tables[0];
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds1.Tables[0];
                }
                else
                {
                    MessageBox.Show("请输入正确信息后查询");
                    goodsInfo_Load(null, null);
                    textBox2.Text = "";
                }
            }
            if (textBox2.Text.Trim() != "" && textBox2.Text.Trim() != "")
            {
                var sql3 = $"select goodsid,GName,UnitPrice,Manufactrue,category from goods where gname like '%{b}%' and goodsid like '%{a}%'";
                var ds3 = DBHelper.show(sql3);
                dataGridView1.DataSource = ds3.Tables[0];
                if (ds3.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds3.Tables[0];
                }
                else
                {
                    MessageBox.Show("请输入正确信息后查询");
                    goodsInfo_Load(null, null);
                    textBox2.Text = "";
                    textBox1.Text = "";
                }
            }
        }
        //删除
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
                var sql = $"delete from goods where goodsid='{id}'";
                int ds = DBHelper.insert(sql);
                if (ds == 1)
                {
                    MessageBox.Show("删除成功");
                    var sql1 = "select goodsid,GName,UnitPrice,Manufactrue,category from goods";
                    DataSet ds1 = DBHelper.show(sql1);
                    dataGridView1.DataSource = ds1.Tables[0];
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
                goodsInfo_Load(null, null);
            }
            else
            {
                MessageBox.Show("请选择后再删除");
            }
        }
    }
}
